package model

import (
	"blogdemo/gin/errmsg"
	"github.com/jinzhu/gorm"
)

type Article struct {
	gorm.Model
	Author       string `gorm:"type:varchar(20); not null" json:"author"`
	Title        string `gorm:"type:varchar(20); not null" json:"title"`
	Desc         string `gorm:"type:varchar(11); not null" json:"desc"`
	Content      string `gorm:"type:longtext; not null" json:"content"`
	Image        string `gorm:"type:varchar(20); not null" json:"image"`
	LikeCount    int    `gorm:"type:int;not null;default:0" json:"like_count"`
	DislikeCount int    `gorm:"type:int;not null;default:0" json:"dislike_count"`
}

// CreateArticle 添加新的文章到article表中
func CreateArticle(theArticle *Article) int {
	err := db.Create(&theArticle).Error
	if err != nil {
		return errmsg.FAIL
	}
	return errmsg.SUCCESS
}

// ChangeArticle 编辑已有的文章
func ChangeArticle(theAuthor string, theTitle string, theArticle *Article) int {
	var maps = make(map[string]interface{})
	maps["title"] = theArticle.Title
	maps["desc"] = theArticle.Desc
	maps["content"] = theArticle.Content
	maps["img"] = theArticle.Image

	err = db.Model(&Article{}).Where("author = ? and title = ?", theAuthor, theTitle).Updates(maps).Error
	if err != nil {
		return errmsg.FAIL
	}
	return errmsg.SUCCESS
}

// GetArticle 以用户名搜素文章
func GetArticle(username string) ([]Article, bool) {
	var article []Article
	db.Where("author = ?", username).Find(&article)
	if len(article) != 0 {
		return article, true
	} else {
		return article, false
	}
}

// EraseArticle 删除文章
func EraseArticle(author string, title string) int {
	var article Article
	db.Where("author = ? and title = ?", author, title).First(&article)
	if article.Title == "" {
		return errmsg.ARTICLE_INEXIST
	}
	db.Unscoped().Where("author = ? and title = ?", author, title).Delete(&article)
	return errmsg.SUCCESS
}

// PlusLike 赞数加1
func PlusLike(author string, title string) int {
	var article Article
	db.Where("author = ? and title = ?", author, title).First(&article)
	likeCount := article.LikeCount + 1
	err := db.Model(&Article{}).Where("author = ? and title = ?",
		author, title).Update("like_count", likeCount).Error
	if err != nil {
		return errmsg.FAIL
	}
	return errmsg.SUCCESS
}

// PlusDislike 踩数加1
func PlusDislike(author string, title string) int {
	var article Article
	db.Where("author = ? and title = ?", author, title).First(&article)
	dislikeCount := article.DislikeCount + 1
	err := db.Model(&Article{}).Where("author = ? and title = ?",
		author, title).Update("dislike_count", dislikeCount).Error
	if err != nil {
		return errmsg.FAIL
	}
	return errmsg.SUCCESS
}

// MinusLike 赞数减1
func MinusLike(author string, title string) int {
	var article Article
	db.Where("author = ? and title = ?", author, title).First(&article)
	likeCount := article.LikeCount - 1
	err := db.Model(&Article{}).Where("author = ? and title = ?",
		author, title).Update("like_count", likeCount).Error
	if err != nil {
		return errmsg.FAIL
	}
	return errmsg.SUCCESS
}

// MinusDislike 踩数减1
func MinusDislike(author string, title string) int {
	var article Article
	db.Where("author = ? and title = ?", author, title).First(&article)
	dislikeCount := article.DislikeCount - 1
	err := db.Model(&Article{}).Where("author = ? and title = ?",
		author, title).Update("dislike_count", dislikeCount).Error
	if err != nil {
		return errmsg.FAIL
	}
	return errmsg.SUCCESS
}

// SearchArticle 以标题关键字搜索文章
func SearchArticle(keyword string) ([]Article, bool) {
	var article []Article
	db.Where("title LIKE ?", "%"+keyword+"%").Find(&article)
	if len(article) != 0 {
		return article, true
	} else {
		return article, false
	}
}
